<template>
  <el-card shadow="hover" class="mt-20">
    <el-button
      type="primary"
      :icon="Plus"
      :disabled="!categoryStore.category3Id"
      @click="showAttrList"
      >添加属性</el-button
    >
    <el-table :data="attrList" border class="mt-20">
      <el-table-column type="index" label="序号" width="60" align="center" />
      <el-table-column prop="attrName" label="属性名" width="150" />
      <el-table-column label="属性值列表">
        <template v-slot="{ row }">
          <el-tag
            class="mr-10"
            type="success"
            v-for="attrValue in row.attrValueList"
            :key="attrValue.id"
          >
            {{ attrValue.valueName }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="operator" label="操作" width="150">
        <!-- <el-button type="warning" size="small" :icon="Edit"></el-button> -->
        <!-- <el-button type="danger" size="small" :icon="Delete"></el-button> -->
        <template v-slot="{ row }">
          <el-button
            type="warning"
            size="small"
            :icon="Edit"
            @click="editAttr(row)"
          ></el-button>
          <el-popconfirm title="您确认要删除吗?" @confirm="delAttr(row.id)">
            <template #reference>
              <el-button type="danger" size="small" :icon="Delete"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "AttrList",
};
</script>

<script lang="ts" setup>
import { Plus, Edit, Delete } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { ref, watch } from "vue";
import { useCategoryStore } from "@/stores/category";
import { GetAttrInfoListApi, DelAttrApi } from "@/api/product/attr";
import type { AttrList, AttrItem } from "@/api/product/model/attrModel";
import { useAttrStore } from "@/stores/attr";

const categoryStore = useCategoryStore();
const attrList = ref<AttrList>([]);

// 封装获取属性信息列表函数
const getAttrInfoList = async () => {
  const { category1Id, category2Id, category3Id } = categoryStore;
  attrList.value = await GetAttrInfoListApi({
    category1Id: category1Id as number,
    category2Id: category2Id as number,
    category3Id: category3Id as number,
  });
};

// 监视三级分类的变化，显示属性信息列表
watch(
  () => categoryStore.category3Id,
  () => {
    const { category3Id } = categoryStore;
    // 判断三级列表id是否为undefined，如果是，属性列表值为空，反则发送获取属性信息列表请求
    if (!category3Id) {
      attrList.value = [];
      return;
    }
    getAttrInfoList();
  },
  // 开启立即监视
  { immediate: true }
);

// 接收父组件传递的自定义事件
const emit = defineEmits(["setShowAttrList"]);
const attrStore = useAttrStore();
// 点击添加属性按钮，调用自定义事件
const showAttrList = () => {
  // 添加属性按钮，传递数据，让属性列表的显示标识变为false，隐藏
  emit("setShowAttrList", false);
  // 重置attrStore数据,不然AttrList展示的是上一次添加的属性值
  attrStore.$reset();
};

// 点击删除按钮触发的回调
const delAttr = async (id: number) => {
  // 发起删除属性请求
  await DelAttrApi(id);
  // 提示删除属性成功
  ElMessage.success("删除属性成功！");
  // 发送获取属性信息列表请求
  getAttrInfoList();
};

// 点击编辑按钮触发的回调
const editAttr = async (row: AttrItem) => {
  // 编辑属性按钮，传递数据，让属性列表的标识变为false，隐藏
  emit("setShowAttrList", false);
  // 通过row获得当行的数据，通过store完成AttrLis组件和AddOrUpdate组件通信
  attrStore.attrItem = row;
  console.log(attrStore.attrItem);
};
</script>

<style scoped></style>
